package com.xuxueli.applyModules.utils.testquarts;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Component
@EnableAsync
public class ServiceScheduler {

    private static final SchedulerFactory SCHEDULER_FACTORY = new StdSchedulerFactory();

    //@Async("serviceBatchSearch")
    public void startSchedule(String serviceName, String batchSearchName, Long batchSearchId, String scheduleTime) {
        try {
            Scheduler scheduler = SCHEDULER_FACTORY.getScheduler();
            JobKey jobKey = new JobKey(batchSearchName + batchSearchId, serviceName);
            JobDetail jobDetail = JobBuilder.newJob(ServiceBatchSearchJob.class).withIdentity(jobKey).build();
            jobDetail.getJobDataMap().put("id", batchSearchId);
            TriggerKey triggerKey = new TriggerKey(batchSearchName + batchSearchId, serviceName);
            Date startDate;
            if (scheduleTime == null) {
                startDate = new Date();
            } else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                startDate = format.parse(scheduleTime);
            }
            //SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(0)
            //    .withRepeatCount(0);
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startDate)
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()).forJob(jobKey).build();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
            log.info("启动定时任务成功: {},{},{}", serviceName, batchSearchId, scheduleTime);
        } catch (ParseException e) {
            log.error("解析定时时间失败,{},{}", scheduleTime, e);
        } catch (SchedulerException e) {
            log.error("启动定时任务失败,{},{}", scheduleTime, e);
        }
    }

    public void updateSchedule(String serviceName, String batchSearchName, Long batchSearchId, String scheduleTime) {
        try {
            Scheduler scheduler = SCHEDULER_FACTORY.getScheduler();
            JobKey jobKey = new JobKey(batchSearchName + batchSearchId, serviceName);
            TriggerKey triggerKey = new TriggerKey(batchSearchName + batchSearchId, serviceName);
            Date startDate;
            if (scheduleTime == null) {
                startDate = new Date();
            } else {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                startDate = format.parse(scheduleTime);
            }
            //SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(0)
            //  .withRepeatCount(0);
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).startAt(startDate)
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()).forJob(jobKey).build();
            scheduler.rescheduleJob(triggerKey, trigger);
            log.info("重置定时任务成功: {},{},{}", serviceName, batchSearchName, scheduleTime);
        } catch (ParseException e) {
            log.error("解析定时时间失败,{},{}", scheduleTime, e);
        } catch (SchedulerException e) {
            log.error("修改定时任务失败,{},{}", scheduleTime, e);
        }
    }

    public void deleteSchedule(String serviceName, String batchSearchName, Long batchSearchId) {
        try {
            Scheduler scheduler = SCHEDULER_FACTORY.getScheduler();
            TriggerKey triggerKey = new TriggerKey(batchSearchName + batchSearchId, serviceName);
            JobKey jobKey = new JobKey(batchSearchName + batchSearchId, serviceName);
            scheduler.pauseTrigger(triggerKey);
            scheduler.unscheduleJob(triggerKey);
            scheduler.deleteJob(jobKey);
            log.info("删除定时任务成功: {},{}", serviceName, batchSearchName);
        } catch (SchedulerException e) {
            log.error("删除定时任务失败: {},{},{}", serviceName, batchSearchName, e);
        }
    }
}
